{extend name="platform/new_base" /}
{block name="resources"/}
{/block}
{block name="main"}
		<form action="" class="form-horizontal form-validate pt-15 widthFixedForm">
			<div class="form-group">
				<label class="col-md-2 control-label"><span class="text-bright">*</span>等级名称</label>
				<div class="col-md-5">
					<input type="text" class="form-control" id="level_name" name="level_name" value="{$info['level_name']}" required>
				</div>
			</div>
			{if $info['is_default']!=1}
			<div class="form-group">
				<label class="col-md-2 control-label"><span class="text-bright">*</span> 升级条件</label>
				<div class="col-md-5">
					<div class="input-group w-400">
						<div class="input-group-addon">成长值达</div>
						<input type="number" id="growth_num" name="growth_num" class="form-control number-form-control " value="{$info['growth_num']}" min="1" autocomplete="off" {if $info['is_default']!=1}required{/if}>
					</div>
                    <div class="mb-0 help-block">数值越大，会员等级越高，只能输入整数</div>
				</div>
				
			</div>
		    {/if}
			<div class="form-group">
				<label class="col-md-2 control-label">优惠折扣</label>
				<div class="col-md-5">
					<input type="number" class="form-control" id="goods_discount" value="{$info['goods_discount']}" min="0.01" max="10">
                    <div class="mb-0 help-block">数值越大，优惠折扣越小, 推荐1-10之间的数字</div>
				</div>
				
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">小数取整</label>
				<div class="col-md-5">
					<div class="switch-inline">
						<input type="checkbox" id="upgrade-switch" value="1" {if $info['is_label']}checked{/if} name="is_label">
						<label for="upgrade-switch"></label>
					</div>
					<div class="help-block mb-0" >折扣后价格小数四舍五入取整，例：198.55取整后199.00，198.46取整后198.00</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label"></label>
				<div class="col-md-8">
					<button class="btn btn-primary addLevel" type="submit">修改</button>
					<a href="javascript:history.go(-1);" class="btn btn-default">返回</a>
				</div>
			</div>
			<input type="hidden" id="level_id" value="{$info['level_id']}">
		</form>
<input type="hidden" id="default_level_name" value="{$info['level_name']}">
<input type="hidden" id="level_growth_num" value="{$level_growth_num}">
<input type="hidden" id="default_growth_num" value="{$info['growth_num']}">
{/block}
{block name="script"}
<script>
    require(['util'],function(util){
        $("input[name='level_name']").on('blur',function () {
            var level_name = $("input[name='level_name']").val();
            var default_level_name = $("#default_level_name").val();
            if(default_level_name!=level_name ){
                $.ajax({
                    type : "post",
                    url : "{:__URL('PLATFORM_MAIN/Member/memberInfo')}",
                    async : true,
                    data : {
                        'level_name' : level_name,
                    },
                    success : function(data) {
                        if (data["code"] > 0) {
                            util.message('该等级已存在','danger');
                            $("input[name='level_name']").val('');
                            return false;
                        }
                    }
                });
			}
        })
        $("input[name='growth_num']").on('blur',function () {
            var default_growth_num = $("#default_growth_num").val();
            var growth_num = $("input[name='growth_num']").val();
            var arr = $("#level_growth_num").val();
            arr=arr.split(',');
            for(var i = 0; i < arr.length; i++){
                if(growth_num === arr[i] && default_growth_num != growth_num){
                    util.message('该等级成长值已存在','danger');
                    $("input[name='growth_num']").val('');
                    return false;
                }
            }
        })
        util.validate($('.form-validate'),function(form){
            var goods_discount = $("#goods_discount").val();
            var level_name = $("#level_name").val();
            var growth_num = $("#growth_num").val();
            var level_id = $("#level_id").val();
            var is_label = $("input[name='is_label']:checked").val();
            if(goods_discount>10){
                util.message('折扣额度不能超过10折','danger');
                return false;
            }
            if(goods_discount<0.01){
                util.message('折扣额度不能小于0.01折','danger');
                return false;
            }
            $('.addLevel').attr({disabled: "disabled"}).html('提交中...');
			$.ajax({
                    type : "post",
                    url : "{:__URL('PLATFORM_MAIN/Member/updateMemberLevel')}",
                    async : true,
                    data : {
                        'level_id' : level_id,
                        'level_name' : level_name,
                        'growth_num' : growth_num,
                        'goods_discount' : goods_discount,
                        'is_label':is_label
                    },
                    success : function(data) {
                        if (data["code"] > 0) {
                            util.message(data["message"],'success',"{:__URL('PLATFORM_MAIN/member/memberLevelList')}");
                        }else if (data["code"] == -2) {
                            util.message('该等级已存在','danger');
                            $('.addLevel').removeAttr('disabled').html('修改');
                        }else{
                            util.message(data["message"],'danger');
                            $('.addLevel').removeAttr('disabled').html('修改');
                        }
                    }
                });
        })
    })
</script>
{/block}